﻿namespace Yom.WebService.Setup
{
    using System;
    using System.Data.SqlClient;
    using System.Text;
    using System.Web;
    using Yom.Common;

    public class SetupSelectDB
    {
        public void OutWrite()
        {
            HttpResponse Response = HttpContext.Current.Response;
            JsonModel Js = new JsonModel();
            StringBuilder strInfo = new StringBuilder();
            string Msg = string.Empty;
            int State = 0;
            string dbhost = Factory.Request("dbhost");
            string dbuser = Factory.Request("dbuser");
            string dbpass = Factory.Request("dbpass");

            if (string.IsNullOrEmpty(dbhost))
                dbhost = "(local)";
            if (string.IsNullOrEmpty(dbuser))
                dbuser = "sa";

            string strConn = "server=" + dbhost + ";uid=" + dbuser + ";pwd=" + dbpass;

            Js.Add("Title", "<p>选择数据库</p>");
            Js.Add("Desc", "<p>选择您要将 Yom网络漫画管理系统 V1.0 Beta 安装到的数据库.</p>");
            strInfo.Append("<div style=\"padding-left: 20px; padding-top: 20px;\">");
            strInfo.Append("<div style=\"padding-left: 20px; padding-top: 10px;\">");
            strInfo.Append("数据库使用选择:<br />");
            strInfo.Append("使用现有数据库: <input type=\"radio\" id=\"selDB\" name=\"selDB\" checked=\"true\" value=\"0\" /><br />");
            strInfo.Append("使用新建数据库: <input type=\"radio\" id=\"selDB\" name=\"selDB\" value=\"1\" /></div>");
            strInfo.Append("<div id=\"nlist\" style=\"padding-left: 20px; padding-top: 10px;\">");
            string nlist = "可用的数据库: ";

            try
            {
                SqlConnection conn = new SqlConnection(strConn);
                SqlDataReader dr;
                SqlCommand cmd = new SqlCommand("select name from master..sysdatabases order by name asc", conn);
                conn.Open();
                conn.ChangeDatabase("master");
                dr = cmd.ExecuteReader();
                nlist += "<select id=\"dblist\">";

                while (dr.Read())
                {
                    string dbname = dr["name"].ToString();

                    if (!string.IsNullOrEmpty(dbname))
                        if (dbname != "master" && dbname != "msdb" && dbname != "tempdb" && dbname != "model" && dbname != "Northwind" && dbname != "pubs")
                            nlist += "<option value=\"" + dbname + "\">" + dbname + "</option>";
                }

                nlist += "</select>";
                conn.Close();
                cmd.Dispose();
                dr.Close();
            }
            catch (Exception ex)
            {
                Msg = "数据库连接错误: " + ex.Message.ToString();
                State = 1;
            }

            strInfo.Append("</div>");
            strInfo.Append("<br /><div id=\"clearDB\" style=\"color:red;padding-left: 20px; padding-top: 10px;\">若选择现有数据库安装, 请先对数据库进行备份, 程序在安装时将会删除所有已存在的漫画表!</div></div>");

            Js.Add("Info", strInfo.ToString());
            Js.Add("Height", "30px");
            Js.Add("State", State);
            Js.Add("Msg", Msg);
            Js.Add("DBList", nlist);
            Js.Add("DBNew", "新数据库名称: <input type=\"text\" id=\"dbnew\" value=\"YomComic\" />");
            Js.Add("Button", "<input type=\"button\" id=\"Previous\" value=\"&lt; 上一步\" />&nbsp;<input type=\"button\" id=\"Next\" value=\"下一步 &gt;\" />");
            Response.Write(new Json(Js));
        }
    }
}